package com.ldk.api.handler;

import com.ldk.api.*;
import com.ldk.base.framework.common.CustomPageable;
import com.ldk.base.framework.util.ConvertUtil;
import com.ldk.framework.entity.Coupon;
import com.ldk.framework.entity.Driver;
import com.ldk.framework.entity.User;
import com.ldk.framework.service.ICouponService;
import com.ldk.framework.service.IDriverService;
import com.ldk.framework.service.IUserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;

import java.util.List;
import java.util.Map;

public class CouponRequestHandler extends BaseRequestHandler {
    @Autowired
    private ICouponService couponService;
    @Autowired
    private IDriverService driverService;
    @Autowired
    private IUserService userService;

    /**
     * 司机获取自己的现金券列表
     *
     * @param parms
     * @return
     * @paramopenId 司机openId
     * @paramstatus 状态: 0 未使用; 1 乘客已使用(司机已提现); -1 已过期
     * @parampageIndex 分页索引
     * @parampageSize 分页每页条数
     */
    @ApiVersion(value = {1})
    @CheckToken(tokenKey = "openId", check = true, platform = RequestPlatform.driver)
    public ResponseObject driverGetSelfCouponList(Map<String, Object> parms) {
        ResponseObject response = ResponseResult.create("coupon", "driverGetSelfCouponList");
        //参数检验
        if (!checkRequireParms(parms, "openId", "pageIndex", "pageSize")) {
            return ResponseResult.exception(response, ErrorCode.REQUEST_PARMS_ERROR);
        }
        String openId = ConvertUtil.parseString(parms.get("openId"));
        if (StringUtils.isBlank(openId)) {
            return ResponseResult.exception(response, ErrorCode.OPENID_EMPTY);
        }
        if (!driverService.existByOpenId("", openId)) {
            return ResponseResult.exception(response, ErrorCode.DRIVER_NOT_EXIST);
        }

        Driver driver = driverService.getDriverByOpenId("", openId);
        if(driver == null){
            return ResponseResult.exception(response, ErrorCode.DRIVER_NOT_EXIST);
        }

        //int status = parms.get("status") != null ? ConvertUtil.parseInt(parms.get("status")) : 0;
        int pageIndex = ConvertUtil.parseInt(parms.get("pageIndex"), 1);
        int pageSize = ConvertUtil.parseInt(parms.get("pageSize"), 10);
        pageIndex = Math.max(pageIndex, 1);
        pageSize = Math.max(pageSize, 1);
        int offset = (pageIndex - 1) * pageSize;
        Sort sort = new Sort(Sort.Direction.DESC, "create_time");
        CustomPageable pageable = new CustomPageable(offset, pageSize, sort);
        Coupon query = new Coupon();
        query.setUserId(driver.getDriverId());

        List<Coupon> result = couponService.selectList("", query, pageable);

        response = ResponseResult.success(response, result);
        return response;
    }

    /**
     * 用户获取优惠券列表接口
     *
     * @param parms
     * @return
     * @paramopenId 用户openId
     * @paramstatus 状态: 0 未使用; 1 乘客已使用(司机已提现); -1 已过期
     * @parampageIndex 分页索引
     * @parampageSize 分页每页条数
     */
    @ApiVersion(value = {1})
    @CheckToken(tokenKey = "openId", check = true, platform = RequestPlatform.client)
    public ResponseObject userGetCouponList(Map<String, Object> parms) {
        ResponseObject response = ResponseResult.create("coupon", "driverGetSelfCouponList");
        //参数检验
        if (!checkRequireParms(parms, "openId", "pageIndex", "pageSize")) {
            return ResponseResult.exception(response, ErrorCode.REQUEST_PARMS_ERROR);
        }
        String openId = ConvertUtil.parseString(parms.get("openId"));
        if (StringUtils.isBlank(openId)) {
            return ResponseResult.exception(response, ErrorCode.OPENID_EMPTY);
        }
        if (!userService.existByOpenId("", openId)) {
            return ResponseResult.exception(response, ErrorCode.USER_NOT_EXIST);
        }

        User user = userService.getUserByOpenId("", openId);
        if(user == null){
            return ResponseResult.exception(response, ErrorCode.USER_NOT_EXIST);
        }

        //int status = parms.get("status") != null ? ConvertUtil.parseInt(parms.get("status")) : 0;
        int pageIndex = ConvertUtil.parseInt(parms.get("pageIndex"), 1);
        int pageSize = ConvertUtil.parseInt(parms.get("pageSize"), 10);
        pageIndex = Math.max(pageIndex, 1);
        pageSize = Math.max(pageSize, 1);
        int offset = (pageIndex - 1) * pageSize;
        Sort sort = new Sort(Sort.Direction.DESC, "create_time");
        CustomPageable pageable = new CustomPageable(offset, pageSize, sort);
        Coupon query = new Coupon();
        query.setUserId(user.getUserId());

        List<Coupon> result = couponService.selectList("", query, pageable);

        response = ResponseResult.success(response, result);
        return response;
    }


    /**
     * 获取现金券/优惠劵信息
     *
     * @param parms
     * @return
     * @param openId openId
     * @param id 现金券/优惠劵ID
     */
    @ApiVersion(value = {1})
    public ResponseObject getCouponInfo(Map<String, Object> parms) {
        ResponseObject response = ResponseResult.create("coupon", "getCouponInfo");
        //参数检验
        if (!checkRequireParms(parms, "openId", "id")) {
            return ResponseResult.exception(response, ErrorCode.REQUEST_PARMS_ERROR);
        }
        String openId = ConvertUtil.parseString(parms.get("openId"));
        String id_str = ConvertUtil.parseString(parms.get("id"));
        if (StringUtils.isBlank(openId)) {
            return ResponseResult.exception(response, ErrorCode.OPENID_EMPTY);
        }
        if (StringUtils.isBlank(id_str)) {
            return ResponseResult.exception(response, ErrorCode.COUPONID_NON_EXIST);
        }
        long id = ConvertUtil.parseLong(id_str);

        Coupon query = new Coupon();
        query.setId(id);

        Coupon result = couponService.selectOne("", query);
        if (result == null) {
            response = ResponseResult.exception(response, ErrorCode.COUPONID_NON_EXIST3);
        } else {
            response = ResponseResult.success(response, result);
        }
        return response;
    }
}
